package com.chenyu.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;


public class ValidationMath {
	public static int RandomMathMax = 0;
	public static int RandomMathMin = 100000;
	public static int RandomMathLength = 6;
	public static String RandomStringMath = "0123456789";
	public static String RandomStringLowercase = "ABCDEFGHIJKMLNOPQRSTUVWXYZ";
	public static String RandomStringCapital = "abcdefghijkmlnopqrstuvwxyz";
	public static String RandomStringChineseCapital = "零壹贰叁肆伍陆柒捌玖拾";
	public static String RandomStringChineseLowercase = "一二三四五六七八九十零";
	public static Map<Integer,String> Months;

	public static Random ran;

	static{
		init();
	}

	/**
	 * 获取一个int整型随机数
	 * @return
	 */
	public static Integer getRandomNumber(){
		return ran.nextInt();
	}

	/**
	 * 获取一个0-指定范围之内的整型随机数(<big)
	 * @param big
	 * @return
	 */
	public static Integer getRandomNumber(int big){
		return ran.nextInt(big);
	}

	/**
	 * 获取一个指定范围的整型随机数(small<=?<big)
	 * @param small
	 * @param big
	 * @return
	 */
	public static Integer getRandomNumber(int small,int big){
		int num =  ran.nextInt(big);
		if(num <= big && num >= small)
			return num;
		return getRandomNumber(small,big);
	}


	/**
	 * Random类中还提供各种类型随机数的方法：
	 nextInt()：返回一个随机整数(int)
	 nextInt(int n)：返回大于等于0、小于n的随机整数(int)
	 nextLong()：返回一个随机长整型值(long)
	 nextBoolean()：返回一个随机布尔型值(boolean)
	 nextFloat()：返回一个随机浮点型值(float)
	 nextDouble()：返回一个随机双精度型值(double)
	 nextGaussian()：概率密度为高斯分布的双精度值(double)
	 * @param key
	 * @param i
	 * @return
	 */
	public static Object GenerateValidation(int key,int...i){
		switch (key) {
			case 1:
				key = ran.nextInt();
				return key;
			case 2:
				key = ran.nextInt(i[0]);
				return key;
			case 3:
				return ran.nextDouble();
			case 4:
				return ran.nextFloat();
			case 5:
				return ran.nextLong();
			case 6:
				return ran.nextBoolean();
			case 7:
				return ran.nextGaussian();
//		case 8:
//			return ran.nextBytes(bytes);;
		}
		return "";
	}

	/**
	 * 初始化
	 */
	public static void init(){
		ran=new Random();
		//Math.round(0.9)四舍五入
		Months = new HashMap<Integer, String>(12);
		Months.put(1, "January");
		Months.put(2, "february");
		Months.put(3, "March");
		Months.put(4, "April");
		Months.put(5, "May");
		Months.put(6, "June");
		Months.put(7, "July");
		Months.put(8, "August");
		Months.put(9, "September");
		Months.put(10, "October");
		Months.put(11, "November");
		Months.put(12, "December");
	}
	/**
	 * 产生长度为length的随机字符串（包括字母和数字）
	 * @param length
	 * @return
	 */
	public static String generateString(int length) {
		StringBuffer sb = new StringBuffer();
		String allChar = RandomStringMath+RandomStringLowercase+RandomStringCapital;
		for (int i = 0; i < length; i++) {
			sb.append(allChar.charAt(ran.nextInt(allChar.length())));
		}
		return sb.toString();
	}
	/**
	 * 产生长度为length的随机字符串（包括字母，不包括数字）
	 * @param length
	 * @return
	 */
	public static String generateMixString(int length) {
		String letterChar = RandomStringLowercase+RandomStringCapital;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append(letterChar.charAt(ran.nextInt(letterChar.length())));
		}
		return sb.toString();
	}
	/**
	 * 产生长度为length的随机小写字符串（包括字母，不包括数字）
	 * @param length
	 * @return
	 */
	public static String generateLowerString(int length) {
		return generateMixString(length).toLowerCase();
	}
	/**
	 * 产生长度为length的随机大写字符串（包括字母，不包括数字）
	 * @param length
	 * @return
	 */
	public static String generateUpperString(int length) {
		return generateMixString(length).toUpperCase();
	}
	/**
	 * 产生长度为length的'0'串
	 * @param length
	 * @return
	 */
	public static String generateZeroString(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append('0');
		}
		return sb.toString();
	}
	/**
	 * 将输入的long整数值补全成为fixedlength长度的字符串
	 * @paramlength
	 * @return
	 */
	public static String toFixdLengthString(long num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(generateZeroString(fixdlenth - strNum.length()));
		} else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常!");
		}
		sb.append(strNum);
		return sb.toString();
	}
	/**
	 * 将输入的int整数值补全成为fixedlength长度的字符串
	 * @paramlength
	 * @return
	 */
	public static String toFixdLengthString(int num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(generateZeroString(fixdlenth - strNum.length()));
		}else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常!");
		}
		sb.append(strNum);
		return sb.toString();
	}

	/**
	 * 随机返回指定字符串中的单个字符
	 * @param str
	 * @return
	 */
	public static char Generate(String str){
		int num = ran.nextInt(str.length());
		return str.substring(num, num+1).charAt(0);
	}

	/**
	 * 生成手机号码{"13","15","17","18"}
	 * @return
	 */
	public static String GeneratePhone(){
		int num = ran.nextInt(4);
		String[] Str ={"13","15","17","18"};
		String str = Str[num];
		for (int i = 0; i < 9; i++) {
			num = ran.nextInt(9);
			str+= RandomStringMath.substring(num, num+1);
		}
		return str;
	}

	/**
	 * 生成邮箱九位以内的QQ和163邮箱
	 * @return
	 */
	public static String GenerateEmails(){
		int num = ran.nextInt(2);
		String[] emails ={"@163.com","@qq.com"};
		String str = emails[num];
		for (int i = 0; i < 9; i++) {
			num = ran.nextInt(9);
			if(num > 0){
				str= RandomStringMath.substring(num, num+1)+str;
			}
		}
		return str;
	}
	/**
	 * 生成邮箱九位以内+格式的指定邮箱号
	 * @return
	 */
	public static String GenerateEmails(String[] emails){
		int num = ran.nextInt(2);
		String str = emails[num];
		for (int i = 0; i < 9; i++) {
			num = ran.nextInt(9);
			if(num > 0){
				str= RandomStringMath.substring(num, num+1)+str;
			}
		}
		return str;
	}

	/**
	 * 返回sex
	 * @return
	 */
	public static String getSex(){
		if(ran.nextBoolean())
			return "女";
		return "男";
	}

	/**
	 * 返回指定大小年龄
	 * @param max
	 * @param min
	 * @return
	 */
	public static Short getAge(Short max,Short min){
		short i = (short)ran.nextInt(max);
		while(!(i >= min && i <= max)){
			i = (short)ran.nextInt(max);
		}
		return i;
	}
}